package com.polaris.live.ui.party.activity

import android.os.Build
import android.os.Bundle
import android.view.WindowManager
import androidx.core.os.bundleOf
import com.polaris.live.R
import com.polaris.live.constant.LiveConst
import com.polaris.live.databinding.ActivityPartyAnchorBinding
import com.polaris.live.floating.FloatingWindowManager
import com.polaris.live.im.sfs.bean.live.LiveRoomInfo
import com.polaris.live.manager.LiveStateManager
import com.polaris.live.ui.live.bean.LiveInitInfo
import com.polaris.live.ui.party.PartyUserStateUtil
import com.polaris.live.ui.party.activity.base.BasePartyAnchorActivity
import com.polaris.live.ui.party.fragment.PartyAnchorFragment
import com.polaris.live.ui.party.listener.OnRoomFinishCallback
import com.polaris.live.utils.fragment.attachFragment
import com.polaris.live.viewmodel.party.PartyAnchorModel

/**
 * PartyAnchorActivity 语聊房主播端
 * @author Created by 天晴 on 2024/2/24 11:13
 * @since 1.0.0
 **/
class PartyAnchorActivity :
    BasePartyAnchorActivity<PartyAnchorModel, ActivityPartyAnchorBinding>() {

    /**
     * 直播间房间信息
     */
    private lateinit var mLiveInfo: LiveRoomInfo
    private val mFragment by lazy {
        supportFragmentManager.attachFragment(R.id.fl_fragment) {
            PartyAnchorFragment().apply {
                arguments = bundleOf(
                    LiveConst.Params.LIVE_INFO to mLiveInfo
                )
                setOnRoomFinishCallback(object : OnRoomFinishCallback {
                    override fun onRoomFinishCallback() {
                        finish()
                    }
                })
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        val initVariable = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            intent.getParcelableExtra(LiveConst.Params.LIVE_INIT_INFO, LiveInitInfo::class.java)
        } else {
            intent.getParcelableExtra(LiveConst.Params.LIVE_INIT_INFO)
        }
        if (initVariable == null) {
            finish()
            return
        }
        LiveStateManager.changeStateForce(LiveConst.State.PARTY)
        mLiveInfo = initVariable.info
        mFragment
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        mViewModel.launchLiveHeartbeat(mLiveInfo.liveId) //心跳

        showMusicWindowView()
    }

    override fun onDestroy() {
        super.onDestroy()
        if (!FloatingWindowManager.isMinimize()) {
            PartyUserStateUtil.inTheMaiSeat = false
            PartyUserStateUtil.siteMicStatus = null
            LiveStateManager.changeState(LiveConst.State.NONE)
        }
    }

    override fun onNetwork(isNetwork: Boolean, isConnected: Boolean) {
        if (this::mLiveInfo.isInitialized) {
            mFragment.onNetwork(isNetwork, isConnected)
        }
    }

    override fun onBackPressed() {
        if (!mFragment.onBackPressed()) {
            super.onBackPressed()
        }
    }
}